<?php
/**
 * 记录集分页
 * 
 * $Id: page.php 122 2010-05-28 10:05:16Z Administrator $
 * $Revision: 122 $
 * $Author: Administrator $
 * $Date: 2010-05-28 18:05:16 +0800 (星期五, 28 五月 2010) $
 * 实现对有效的记录集进行分类，比如数据库的查询记录，一个目录下的文件等。
 * 
 * @author glzaboy<glzaboy@163.com>
 * @package fastlib
 * @subpackage classes
 * @copyright 2005-2010 (c) glzaboy
 */
if (! defined ( '__FASTLIB__' )) {
	if (__DEBUG__) {
		die ( sprintf ( 'Forbidden 403 at file %s on line %d', __FILE__, __LINE__ ) );
	} else {
		die ( 'Forbidden 403' );
	}
}
/**
 * 记录集分页
 *
 * 实现对有效的记录集进行分类，比如数据库的查询记录，一个目录下的文件等。
 */
class page {
	/**
	 * 记录总数
	 *
	 * @var Int
	 */
	private $itemCount = null;
	/**
	 * 分页配置
	 *
	 * @var Array
	 */
	private $pageConfig = null;
	/**
	 * 分页输出格式0（首页）1（上一页）2（下一页）3（未页）4（共页）5（共条）
	 * 6（跳转，只用于wml） 7(询问要改到那页只用于wml)
	 *
	 * @var String
	 */
	private $outFormat = null;
	/**
	 * 元素使用的样式
	 *
	 * @var unknown_type
	 */
	private $curFormat = null;
	/**
	 * 分页元素,共有0-6,list
	 *
	 * @var String
	 */
	private $outArray = array ();
	/**
	 * 当前页数
	 *
	 * @var Int
	 */
	private $currentPage = null;
	/**
	 * 总页数
	 *
	 * @var Int
	 */
	private $maxPage = null;
	/**
	 * 当前页记录在记录数的偏移开始
	 *
	 * @var Int
	 */
	public $offset = null;
	/**
	 * 当前页共取记录数
	 *
	 * @var Int
	 */
	public $num = null;
	/**
	 * 生成页的程序名
	 *
	 * @var Int
	 */
	private $PHP = null;
	/**
	 * 构造类
	 *
	 * pageConfig的配置代码
	 * <code>
	 * $flib_cfg['pagecfg']=array(
	 * "style"=>array("Home","Previous","Next","Last","%d page(s)","%d item(s)","GO","Which page you want?"),//分页提示
	 * "field"=>"page",//分页字段
	 * "show"=>5,//显示多少个可以直接点击的跳转
	 * "num"=>10,//显示的分页数
	 * "outFormat"=>'0123456-',//输出格式，一般直接高show就可以了。
	 * "jscallback"=>null,//点击时执行JS，自动return false
	 * "class"=>"",//应该的CSS
	 * );
	 * </code>
	 * 
	 * @param Array $pageConfig 页面配置
	 * @param string $SCRIPT_NAME 生成页面的脚本名
	 * @return page
	 */
	public function __construct($SCRIPT_NAME = null) {
		global $flibcfg;
		$this->pageConfig = $flibcfg ['pagecfg'];
		$this->PHP = ($SCRIPT_NAME !== null) ? $SCRIPT_NAME : array_pop ( explode ( '/', $_SERVER ['SCRIPT_NAME'] ) );
		return true;
	}
	/**
	 * 分页计算，完成后产生pagepage::offset,page:maxpage等
	 *
	 * @param Int $itemCount 总记录数
	 */
	public function parser($itemCount = 1) {
		$this->itemCount = $itemCount > 0 ? intval ( $itemCount ) : 0.5;
		$get = $_GET;
		if ($this->pageConfig ['field'] == null) {
			$this->pageConfig ["field"] = "page";
		}
		if ($this->pageConfig ['show'] === 0) {
		} elseif ($this->pageConfig ['show'] === null) {
			$this->pageConfig ['show'] = 10;
		}
		if (! $this->pageConfig ['num']) {
			$this->pageConfig ['num'] = 20;
		}
		$this->currentPage = @intval ( $_GET [$this->pageConfig ['field']] );
		if ($this->currentPage == null) {
			$this->currentPage = 1;
		}
		$this->maxPage = ceil ( $this->itemCount / $this->pageConfig ['num'] );
		if (! is_array ( $this->pageConfig ['style'] )) {
			$this->pageConfig ['style'] = array ('First', 'Previous', 'Next', 'Last', ' %d page(s)', '%d items pre page', 'Total %d item(s)', 'Go', 'Which page go?' );
		}
		if (isset ( $this->pageConfig ['outFormat'] )) {
			$this->outFormat = trim ( $this->pageConfig ['outFormat'] );
		} elseif ($_SESSION ['wml'] == 1) {
			$this->outFormat = "213046-7";
			$this->pageConfig ['jscallback'] = null;
			$this->pageConfig ['class'] = null;
		} else {
			$this->outFormat = "01-23475";
		}
		if ($this->pageConfig ['class'] !== null && $_SESSION ['wml'] != 1) {
			if ($_SESSION ['wml'] != 1 && $this->pageConfig ['class'] ['current']) {
				$this->curFormat = "<span style=\"color:gray;\" class=\"{$this->pageConfig['class']['current']}\">%s</span>";
			} else {
				$this->curFormat = "<span style=\"color:gray;\">%s</span>";
			}
			if ($_SESSION ['wml'] != 1 && $this->pageConfig ['class'] ['text']) {
				$this->textFormat = "<span class=\"{$this->pageConfig['class']['text']}\">%s</span>";
			} else {
				$this->textFormat = "<span>%s</span>";
			}
		} else {
			$this->curFormat = "%s";
		}
		if ($this->currentPage <= 1) { //|<<
			if ($_SESSION ['wml'] != 1) {
				$this->outArray [0] = sprintf ( $this->curFormat, $this->pageConfig ['style'] [0] );
			}
		} else {
			$this->outArray ['0'] = '<a href=';
			$get [$this->pageConfig ['field']] = 1;
			$this->outArray ['0'] .= '"' . $this->PHP . '?' . http_build_query ( $get ) . '"';
			if ($this->pageConfig ['jscallback'] && $_SESSION ['wml'] != 1) {
				$this->outArray ['0'] .= " onclick=\"{$this->pageConfig['jscallback']}(this);return false;\"";
			}
			if ($this->pageConfig ['class'] && $_SESSION ['wml'] != 1) {
				$this->outArray ['0'] .= " class=\"{$this->pageConfig['class']['default']}\"";
			}
			$this->outArray ['0'] .= '>' . $this->pageConfig ['style'] [0] . '</a> ';
		}
		if ($this->currentPage <= 1) { //|<
			$this->outArray [1] = sprintf ( $this->curFormat, $this->pageConfig ['style'] [1] );
		} else {
			$this->outArray ['1'] = '<a href=';
			$get [$this->pageConfig ['field']] = $this->currentPage - 1;
			$this->outArray ['1'] .= '"' . $this->PHP . '?' . http_build_query ( $get ) . '"';
			if ($this->pageConfig ['jscallback'] && $_SESSION ['wml'] != 1) {
				$this->outArray ['1'] .= " onclick=\"{$this->pageConfig['jscallback']}(this);return false;\"";
			}
			if ($this->pageConfig ['class'] && $_SESSION ['wml'] != 1) {
				$this->outArray ['1'] .= " class=\"{$this->pageConfig['class']['default']}\"";
			}
			$this->outArray ['1'] .= '>' . $this->pageConfig ['style'] [1] . '</a> ';
		}
		$this->outArray ['list'] = '';
		$tmp = ( int ) ($this->pageConfig ['show'] / 2);
		for($tmpx = 0; $tmpx < $this->pageConfig ['show']; $tmpx ++) { //num
			if (($this->currentPage - $tmp + $tmpx) > $this->maxPage or ($this->currentPage - $tmp + $tmpx) <= 0) {
				continue;
			}
			if ($this->currentPage == ($this->currentPage - $tmp + $tmpx)) {
				$this->outArray ['list'] .= sprintf ( $this->curFormat, $this->currentPage - $tmp + $tmpx ) . " ";
				continue;
			}
			$this->outArray ['list'] .= '<a href=';
			$get [$this->pageConfig ['field']] = $this->currentPage - $tmp + $tmpx;
			$this->outArray ['list'] .= '"' . $this->PHP . '?' . http_build_query ( $get ) . '"';
			if ($this->pageConfig ['jscallback'] && $_SESSION ['wml'] != 1) {
				$this->outArray ['list'] .= " onclick=\"{$this->pageConfig['jscallback']}(this);return false;\"";
			}
			if ($this->pageConfig ['class'] && $_SESSION ['wml'] != 1) {
				$this->outArray ['list'] .= " class=\"{$this->pageConfig['class']['default']}\"";
			}
			$this->outArray ['list'] .= '>' . ($this->currentPage - $tmp + $tmpx) . '</a> ';
		}
		$this->outArray ['list'] = preg_replace ( '/ $/', '', $this->outArray ['list'] );
		if ($this->currentPage >= $this->maxPage) { //>|
			$this->outArray [2] = sprintf ( $this->curFormat, $this->pageConfig ['style'] [2] );
		} else {
			$this->outArray [2] .= '<a href=';
			$get [$this->pageConfig ['field']] = $this->currentPage + 1;
			$this->outArray [2] .= '"' . $this->PHP . '?' . http_build_query ( $get ) . '"';
			if ($this->pageConfig ['jscallback'] && $_SESSION ['wml'] != 1) {
				$this->outArray [2] .= " onclick=\"{$this->pageConfig['jscallback']}(this);return false;\"";
			}
			if ($this->pageConfig ['class'] && $_SESSION ['wml'] != 1) {
				$this->outArray [2] .= " class=\"{$this->pageConfig['class']['default']}\"";
			}
			$this->outArray [2] .= '>' . $this->pageConfig ['style'] [2] . '</a> ';
		}
		if ($this->currentPage >= $this->maxPage) { //>>|
			$this->outArray [3] = sprintf ( $this->curFormat, $this->pageConfig ['style'] [3] );
		} else {
			$this->outArray [3] .= '<a href=';
			$get [$this->pageConfig ['field']] = $this->maxPage;
			$this->outArray [3] .= '"' . $this->PHP . '?' . http_build_query ( $get ) . '"';
			if ($this->pageConfig ['jscallback'] && $_SESSION ['wml'] != 1) {
				$this->outArray [3] .= " onclick=\"{$this->pageConfig['jscallback']}(this);return false;\"";
			}
			if ($this->pageConfig ['class'] && $_SESSION ['wml'] != 1) {
				$this->outArray [3] .= " class=\"{$this->pageConfig['class']['default']}\"";
			}
			$this->outArray [3] .= '>' . $this->pageConfig ['style'] [3] . '</a> ';
		}
		$this->outArray [4] = sprintf ( sprintf ( $this->textFormat, $this->pageConfig ['style'] [4] ), $this->maxPage );
		$this->outArray [5] = sprintf ( sprintf ( $this->textFormat, $this->pageConfig ['style'] [6] ), $this->itemCount );
		$this->outArray [6] = "<input type=\"text\" name=\"fastlib{$this->pageConfig['field']}\" size=\"3\" title=\"{$this->pageConfig['style'][8]}\" format=\"*N\"/>\n";
		$this->outArray [6] .= "<anchor>" . $this->pageConfig ['style'] [7] . "<go href=\"{$this->PHP}\" method=\"get\">";
		$this->outArray [6] .= "<postfield name=\"{$this->pageConfig['field']}\" value=\"$(fastlib{$this->pageConfig['field']})\"/>";
		unset ( $get [$this->pageConfig ['field']] );
		if (count ( $get )) {
			$getfield = http_build_query ( $get );
		}
		if (defined ( 'SID' ) && SID != '') {
			$getfield .= '&amp;' . SID;
		}
		$getfield = strtr ( $getfield, array ('&amp;' => '&' ) );
		$this->outArray [6] .= '<!--' . formfield ( $getfield, false, array ($this->pageConfig ['field'] ) ) . '-->';
		$this->outArray [6] .= '</go></anchor>';
		$this->outArray [7] = sprintf ( sprintf ( $this->textFormat, $this->pageConfig ['style'] [5] ), $this->pageConfig ['num'] ); //go wap
		$this->offset = ($this->currentPage - 1) * $this->pageConfig ['num'];
		$this->num = $this->pageConfig ['num'];
	}
	/**
	 * 返回分页的字符串
	 *
	 * @param bool $parser 自动执行parser，当忘记执行时自动执行
	 * @return String
	 */
	public function show($parser = false) {
		if ($parser && $this->currentPage == null) {
			$this->parser ();
		}
		$pagestr = '';
		$iFormatLen = strlen ( $this->outFormat );
		for($x = 0; $x < $iFormatLen; $x ++) {
			if (! ($x == 0 or $x == $iFormatLen)) {
				$pagestr .= " ";
			}
			if ($this->outFormat {$x} == "-") {
				$pagestr .= $this->outArray ['list'];
			} else {
				$pagestr .= $this->outArray [$this->outFormat {$x}];
			}
		}
		return $pagestr;
	}
	/**
	 * 返回本模块信息
	 *
	 * @return string
	 */
	function printInfo() {
		$version = '$Revision: 122 $';
		$moudle = __CLASS__;
		$return = '';
		$return .= startTable ();
		$return .= printRow ( array (array ('class' => 'h', 'content' => '<center>' . $moudle . '</center>', 'colspan' => '2' ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'author' ), array ('class' => 'v', 'content' => 'glzaboy' ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'version' ), array ('class' => 'v', 'content' => $version ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'support' ), array ('class' => 'v', 'content' => 'HTML,xhtml,wml,Multiple pages on one page' ) ) );
		$return .= endTable ();
		return $return;
	}
}